package com.kye.express.business.waybill_take_photo.history;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.fondesa.recyclerviewdivider.RecyclerViewDivider;
import com.github.chrisbanes.photoview.OnPhotoTapListener;
import com.kye.express.R;
import com.kye.express.business.base.BaseToolbarActivity;
import com.kye.express.databinding.ActivityWaybillTakePhotoHistoryBinding;
import com.kye.express.utils.ImageUtils;
import com.kye.express.utils.ScreenUtils;
import com.kye.express.view.KyeProgressDialog;
import com.kye.express.view.ToastManager;
import com.kye.model.bean.waybill_take_photo.QueryWaybillImageResultBean;
import com.kye.model.bean.waybill_take_photo.QueryWaybillTakePhotoHistoryResultBean;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.List;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

/**
 * 运单上传记录
 */
public class WaybillTakePhotoHistoryActivity extends BaseToolbarActivity implements WaybillTakePhotoHistoryActivityContract.WaybillTakePhotoHistoryActivityView {
    private ActivityWaybillTakePhotoHistoryBinding mBinding;

    protected MultiTypeAdapter mAdapter;
    protected Items mDatas = new Items();
    private Dialog mLoadingDialog;

    private WaybillTakePhotoHistoryActivityContract.WaybillTakePhotoHistoryActivityViewPresenter mPresenter;

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, WaybillTakePhotoHistoryActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_waybill_take_photo_history);
        mPresenter = new WaybillTakePhotoHistoryActivityContract.WaybillTakePhotoHistoryActivityViewPresenter(this);
        mPresenter.bind(this);
        setUpToolbar((Toolbar) findViewById(R.id.toolbar));
        setTitle(getString(R.string.waybill_take_photo_upload_history));
        initView();
        initEvent();
        mPresenter.loadDatas(true, false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.unbind();
        }
    }

    private void initView() {
        mAdapter = new MultiTypeAdapter();
        mAdapter.register(QueryWaybillTakePhotoHistoryResultBean.DatasBean.class, new WaybillTakePhotoHistoryListItemViewBinder(this, mPresenter));
        mBinding.recyclerview.setLayoutManager(new LinearLayoutManager(this));
        mBinding.recyclerview.setHasFixedSize(true);
        mBinding.recyclerview.setNestedScrollingEnabled(false);
        RecyclerViewDivider.with(this)
                .color(getResources().getColor(R.color.grey))
                .size(ScreenUtils.dp2px(this, 1))
                .hideLastDivider()
                .build()
                .addTo(mBinding.recyclerview);
        mBinding.recyclerview.setItemAnimator(new DefaultItemAnimator());
        mAdapter.setItems(mDatas);
        mBinding.recyclerview.setAdapter(mAdapter);
        mBinding.refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                mPresenter.loadDatas(false, false);
            }

            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                mPresenter.loadDatas(false, true);
            }

        });
    }

    private void initEvent() {

    }

    private void showToast(String message) {
        ToastManager.show(WaybillTakePhotoHistoryActivity.this, message, Toast.LENGTH_SHORT);
    }

    @Override
    public void onComplete(boolean isLoadMore) {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
        }
        if (isLoadMore) {
            mBinding.refreshLayout.finishLoadmore();
        } else {
            mBinding.refreshLayout.finishRefresh();
        }
    }

    @Override
    public void onLoading() {
        try {
            if (mLoadingDialog == null) {
                mLoadingDialog = new KyeProgressDialog.Builder(this)
                        .build();
            }
            mLoadingDialog.show();
        } catch (Exception e) {

        }
    }

    @Override
    public void onLoaded(QueryWaybillTakePhotoHistoryResultBean resultDatas, boolean isLoadMore) {
        if (resultDatas != null) {
            List<QueryWaybillTakePhotoHistoryResultBean.DatasBean> payRecordBeans = resultDatas.getDatas();
            if (payRecordBeans != null && payRecordBeans.size() > 0) {
                if (!isLoadMore) {
                    mDatas.clear();
                    mDatas.addAll(payRecordBeans);
                    mAdapter.notifyDataSetChanged();
                } else {
                    List<QueryWaybillTakePhotoHistoryResultBean.DatasBean> datas = (List<QueryWaybillTakePhotoHistoryResultBean.DatasBean>) mAdapter.getItems();
                    if (datas != null) {
                        int start = datas.size();
                        datas.addAll(payRecordBeans);
                        mAdapter.notifyDataSetChanged();
                    }
                }
            }
        }
    }

    @Override
    public void onError(String msg, boolean isLoadMore) {
        ToastManager.show(this, msg, Toast.LENGTH_SHORT);
        if (isLoadMore) {
            mBinding.refreshLayout.finishLoadmore(false);
        } else {
            mBinding.refreshLayout.finishRefresh(false);
        }
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
        }
    }

    @Override
    public void onError(String msg) {
        ToastManager.show(this, msg, Toast.LENGTH_SHORT);
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
        }
    }

    @Override
    public void onComplete() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
        }
    }

    @Override
    public void onLoadedImage(QueryWaybillImageResultBean datas) {
        if (datas != null){
            String imageBase64 = datas.getImage();
            Bitmap bitmap = ImageUtils.Base64StringToBitmap(imageBase64);
            if (bitmap != null){
                mBinding.photoview.setVisibility(View.VISIBLE);
                mBinding.photoview.setImageBitmap(bitmap);
                mBinding.photoview.setOnPhotoTapListener(new OnPhotoTapListener() {
                    @Override
                    public void onPhotoTap(ImageView view, float x, float y) {
                        mBinding.photoview.setVisibility(View.GONE);
                    }
                });
            }
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            if (mBinding.photoview.getVisibility() == View.VISIBLE){
                mBinding.photoview.setVisibility(View.GONE);
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
